package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.service.ITravelStatisRedisService;
import cn.wolfcode.luowowo.cache.until.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.vo.TravelStatisVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * Created by LENOVO on 2019/8/15.
 */
@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent>{//实现监听器的接口,然后ContextRefreshedEvent这个泛型就是当容器启动就执行


    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;


    @Reference
    private ITravelService travelService;

    @Reference
    private ITravelStatisRedisService travelStatisRedisService;







    //Spring容器启动后马上启动
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        System.out.println("-------------------------");
        //重mysql中获取所有的攻略明细数据
        List<StrategyDetail> list = strategyDetailService.list();
       //设置一个VO 对象
        StrategyStatisVO vo=null;

        for(StrategyDetail detail:list){
            //如果该数据已经出现在redis中了,我们需要跳过,否则数据将会覆盖
            if(strategyStatisRedisService.isStrategyStatisVOEXsit(detail.getId())){
                continue;//跳过当前的循环
            }
            vo=new StrategyStatisVO();
            //遍历添加到redis中
            try {
                BeanUtils.copyProperties(vo,detail);
                vo.setDestId(detail.getDest().getId());
                vo.setDestName(detail.getDest().getName());
                vo.setStrategyId(detail.getId());//攻略的id
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            //设置
            strategyStatisRedisService.setStrategyStatisVO(vo);
        }


        //游记数据的初始化到redis中

        //重mysql中获取所有的攻略明细数据
        List<Travel> tlist = travelService.list();
        //设置一个VO 对象
        TravelStatisVO tvo=null;

        for(Travel travel:tlist){
            //如果该数据已经出现在redis中了,我们需要跳过,否则数据将会覆盖
            if(travelStatisRedisService.isStrategyStatisVOEXsit(travel.getId())){
                continue;//跳过当前的循环
            }
            tvo=new TravelStatisVO();
            //遍历添加到redis中
            try {
                BeanUtils.copyProperties(tvo,travel);
                tvo.setDestId(travel.getDest().getId());
                tvo.setDestName(travel.getDest().getName());
                tvo.setTravelId(travel.getId());//攻略的id
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            //设置
            travelStatisRedisService.setTravelStatisVO(tvo);



        }

        System.out.println("--------------zet的初始化");
            for(StrategyDetail detail:list){
                String value=RedisKeys.STRATEGY_STATIS_VO.join(detail.getId().toString());
                //参数一就是zset的key   ,参数二是zset的value
                if(strategyStatisRedisService.isZsetHasValue(RedisKeys.STRATEGY_STATIS_COMMEND_SORT.getPrefix(),value)){
                    continue;
                }
                //点赞数+收藏数
                int score = detail.getThumbsupnum() + detail.getFavornum();
                strategyStatisRedisService.addCommendScores(value,score);

            }

            //热门排行的初始化
                for(StrategyDetail detail:list){
                        String value=RedisKeys.STRATEGY_STATIS_VO.join(detail.getId().toString());
                    if(strategyStatisRedisService.isZsetHasValue(RedisKeys.STRATEGY_STATIS_HOT_SORT.getPrefix(),value)){
                        continue;
                    }

                    int score=detail.getViewnum()+detail.getReplynum();
                    strategyStatisRedisService.addHotScore(value,score);

                }

        System.out.println("--------------zet的end");

    }
}
